//
//  ShootViewController.m
//  Dear
//
//  Created by n22 on 15/8/17.
//  Copyright (c) 2015年 陈俊俊. All rights reserved.
//

#import "ShootViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "ShootResultView.h"
#import "UIImageView+WebCache.h"
@interface ShootViewController ()<ShootResultViewDelegate>
{
    BOOL _isLoading;
    NSString *_type;
    ShootResultView *_resultView;
}
@property (nonatomic, strong) AVAudioPlayer * player;
@end

@implementation ShootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    [self createTitleViewWith:@"摇一摇"];
    [self dataInit];
    
}
- (void) dataInit {
    _isLoading = NO;
    //默认第一个选中
    self.girlBtn.selected = YES;
    _type = @"1";
    [self prepAudio];
}
- (BOOL) prepAudio {
    NSString * path = [[NSBundle mainBundle] pathForResource:@"shake_sound_male" ofType:@"wav"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:path] || !path) {
        return NO;
    }
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
    [self.player prepareToPlay];
    [self.player setNumberOfLoops:0];
    return YES;
}
- (IBAction)btnClick:(id)sender {
    UIButton *btn = (UIButton *)sender;
    switch (btn.tag) {
        case 6000://美女
        {
            self.jokesBtn.selected = NO;
            self.funcBtn.selected = NO;
            self.girlBtn.selected = YES;
            _type = @"1";
        }
            break;
        case 6001://段子
        {
            self.jokesBtn.selected = YES;
            self.funcBtn.selected = NO;
            self.girlBtn.selected = NO;
            _type = @"0";
            
        }
            break;
        case 6002://趣图
        {
            self.jokesBtn.selected = NO;
            self.funcBtn.selected = YES;
            self.girlBtn.selected = NO;
            _type = @"2";
            
        }
            break;
        default:
            break;
    }
}

//摇晃前
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    [self dissmissFromView];
 
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{

}
//摇晃后
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake) {
        [self.player play];
       [UIView animateWithDuration:2 animations:^{
           self.bgImageView.image = [UIImage imageNamed:@"bgshoot1"];
       }completion:^(BOOL finished) {
//           [self getHttpData];
           [self performSelector:@selector(getHttpData) withObject:nil afterDelay:1];
       }];
    }
}
- (void)getHttpData{
    self.bgImageView.image = [UIImage imageNamed:@"bgshoot2"];
    if (!_isLoading) {
        [self getShakeData];
    }
}

- (void)getShakeData{
    _isLoading = YES;
    NSDictionary *dict = @{@"userId":[DearUser sharedInstance].userId,@"type":_type};
    NSString *url = [ServerUrl stringByAppendingFormat:@"/%@",DearHttpYao];
    [[HttpRequest sharedInstance] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [DearLoadingView hideHUD];
        _isLoading = NO;
        NSDictionary *dict = (NSDictionary *)responseObject;
        if ([dict[@"Result"] integerValue] == 0)
        {
            NSLog(@"%@",responseObject);
            //显示视图
            [self showResultView:dict];
        }else {
            [DearLoadingView showTips:dict[@"Message"]];
        }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        _isLoading = NO;
        [DearLoadingView hideHUD];
        [DearLoadingView showTips:error];
    }];

}


#pragma mark - 显示结果
- (void)showResultView:(NSDictionary *)dict{
    if ([_type isEqualToString:@"0"]) {//段子
        if (_resultView == nil) {
            _resultView = [[ShootResultView alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width, ScreenSize.height)];
            _resultView.delegate = self;
            [[DearAppDelegate window] addSubview:_resultView];
            UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(10, (ScreenSize.height - 200)/2, ScreenSize.width - 20, 200)];
            textView.text = dict[@"description"];
            textView.layer.cornerRadius = 8;
            textView.font = FontSize(17);
            textView.editable = NO;
            textView.layer.masksToBounds = YES;
            [_resultView addSubview:textView];
        }
        
    }else//美女
    {
        if (_resultView == nil) {
            _resultView = [[ShootResultView alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width, ScreenSize.height)];
            _resultView.delegate = self;
            [[DearAppDelegate window] addSubview:_resultView];
           UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 64, ScreenSize.width - 20, ScreenSize.height - 64 -50)];
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            NSString *picture = dict[@"picture"];
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@?path=%@",ServerUrl,DearImage,picture]];
            [imageView sd_setImageWithURL:url];
            [_resultView addSubview:imageView];
        }
    }
}

- (void)dissmissFromView{
    if (_resultView) {
        [_resultView removeFromSuperview];
        _resultView = nil;
    }
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //当前vc设定为第一响应者
    [self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
    [self resignFirstResponder];
    [super viewWillDisappear:animated];
}
-(BOOL)canBecomeFirstResponder {
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
